package com.itheima.service.db.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.domain.UmsMenu;
import com.itheima.mapper.UmsMenuMapper;
import com.itheima.service.db.UmsMenuService;
import com.itheima.vo.PageBeanVo;
import com.itheima.vo.UmsMenuVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@DubboService
public class UmsMenuServiceImpl implements UmsMenuService {

    @Autowired
    UmsMenuMapper umsMenuMapper;

    @Override
    public UmsMenu findById(Long id) {
        return umsMenuMapper.selectById(id);
    }

    /**
     * 树形结构返回所有菜单列表
     * @return
     */
    @Override
    public List<UmsMenuVo> getTreeList() {
        // 创建构造器,查询一级菜单
        LambdaQueryWrapper<UmsMenu> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UmsMenu::getParentId,0);
        List<UmsMenu> umsMenus1 = umsMenuMapper.selectList(queryWrapper);
        List<UmsMenuVo> voList = new ArrayList<>();
        // 遍历一级菜单
        if (CollUtil.isNotEmpty(umsMenus1)) {
            for (UmsMenu umsMenu : umsMenus1) {
                // 获取id,通过id查询子类
                UmsMenuVo vo = new UmsMenuVo();
                Long id = umsMenu.getId();
                queryWrapper = new LambdaQueryWrapper<>();
                queryWrapper.eq(UmsMenu::getParentId,id);
                List<UmsMenu> children = umsMenuMapper.selectList(queryWrapper);

                // 将一级分类的信息复制到vo类中
                BeanUtil.copyProperties(umsMenu,vo);
                vo.setChildren(children);

                // 将信息保存到voList中
                voList.add(vo);
            }
        }
        return voList;
    }

    /**
     * 分页查询后台菜单
     * @param parentId
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public PageBeanVo getList(Long parentId, Integer pageNum, Integer pageSize) {
        // 创建条件构造器
        LambdaQueryWrapper<UmsMenu> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UmsMenu::getParentId,parentId).orderByDesc(UmsMenu::getSort);
        List<UmsMenu> umsMenus = umsMenuMapper.selectList(queryWrapper);

        // 创建分页查询器
        Page page = new Page(pageNum,pageSize);
        page.setTotal(umsMenus.size());
        page.setRecords(umsMenus);

        return new PageBeanVo(pageNum,pageSize,page.getTotal(),page.getRecords());
    }

    /**
     * 添加后台菜单
     * @param umsMenu
     */
    @Override
    public void createBackgroundMenu(UmsMenu umsMenu) {
        umsMenu.setCreateTime(LocalDateTime.now());
        LambdaQueryWrapper<UmsMenu> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UmsMenu::getId,umsMenu.getParentId());
        // 根据parentId去查询Umsenu
        List<UmsMenu> umsMenus = umsMenuMapper.selectList(queryWrapper);
        if (CollUtil.isNotEmpty(umsMenus)) {
            for (UmsMenu menu : umsMenus) {
                if (menu.getLevel() == 0) {
                    umsMenu.setLevel(1);
                }
            }
        } else {
            umsMenu.setLevel(0);
        }
        umsMenuMapper.insert(umsMenu);
    }

    /**
     * 根据ID获取菜单详情
     * @param id
     * @return
     */
    @Override
    public UmsMenu getMenuDetails(Long id) {
        // 创建条件构造器
        LambdaQueryWrapper<UmsMenu> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UmsMenu::getId,id);
        UmsMenu umsMenu = umsMenuMapper.selectOne(queryWrapper);
        return umsMenu;
    }

    /**
     * 修改后台菜单
     * @param id
     * @param umsMenu
     */
    @Override
    public void updateMenu(Long id, UmsMenu umsMenu) {
        umsMenu.setId(id);
        umsMenuMapper.updateById(umsMenu);
    }

    @Override
    public void updateHidden(Long id, Integer hidden) {
        // 创建条件构造器
        LambdaQueryWrapper<UmsMenu> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UmsMenu::getId,id);
        UmsMenu umsMenu = umsMenuMapper.selectOne(queryWrapper);
        umsMenu.setHidden(hidden);
        umsMenuMapper.updateById(umsMenu);
    }

    /**
     * 根据ID删除后台菜单
     * @param id
     */
    @Override
    public Boolean delete(Long id) {
        /*// 创建条件构造器
        LambdaQueryWrapper<UmsMenu> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UmsMenu::getId,id);
        umsMenuMapper.delete(queryWrapper);*/

        LambdaQueryWrapper<UmsMenu> lqw=new LambdaQueryWrapper<>();
        lqw.eq(UmsMenu::getParentId,id);
        List<UmsMenu> umsMenus = umsMenuMapper.selectList(lqw);
        if (umsMenus.size() != 0){
            return false;
        }
        umsMenuMapper.deleteById(id);
        return true;
    }
}
